package com.bytz.modules.cms.product.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bytz.common.api.vo.Result;
import com.bytz.common.system.base.controller.BytzController;
import com.bytz.common.system.query.QueryGenerator;
import com.bytz.modules.cms.common.constants.AdminAuthConstants;
import com.bytz.modules.cms.product.entity.ProductDiscountCategory;
import com.bytz.modules.cms.product.model.ProductDiscountCategoryDetail;
import com.bytz.modules.cms.product.service.IAdminProductDiscountCategoryService;
import com.bytz.modules.cms.product.service.IProductDiscountCategoryLevelService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @BelongsProject: cms-backend
 * @BelongsPackage: com.bytz.modules.cms.product.controller
 * @Author: dst
 * @CreateTime: 2022-10-27  16:01
 * @Description: TODO
 * @Version: 1.0
 */
@RestController
@Api(tags = "分类管理")
@RequestMapping("/productDiscountCategory")
public class AdminProductDiscountCategoryController extends BytzController<ProductDiscountCategory, IAdminProductDiscountCategoryService> {

    @Autowired
    private IAdminProductDiscountCategoryService productDiscountCategoryService;

    @Autowired
    private IProductDiscountCategoryLevelService productDiscountCategoryLevelService;

    @ApiOperation(value = "分类管理-分页查询", notes = "分类管理-分页查询")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @PreAuthorize("hasAuthority('" + AdminAuthConstants.ProductDiscountCategory.EDIT + "')")
    public Result<IPage<ProductDiscountCategory>> page(ProductDiscountCategory productDiscountCategory,
                                                       @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                       @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                                       HttpServletRequest req) {

        return Result.ok(productDiscountCategoryService.page(
                new Page<>(pageNo, pageSize),
                QueryGenerator.initQueryWrapper(productDiscountCategory, req.getParameterMap())
        ));
    }

    @ApiOperation(value = "分类管理-全部分类列表", notes = "分类管理-全部分类列表")
    @RequestMapping(value = "/listAll", method = RequestMethod.GET)
    public Result<List<ProductDiscountCategory>> list() {

        return Result.ok(productDiscountCategoryService.getByLoginUser());
    }

    @ApiOperation(value = "分类管理-根据产品线查找分类", notes = "分类管理-根据产品线查找分类")
    @RequestMapping(value = "/listByLine", method = RequestMethod.GET)
    public Result<List<ProductDiscountCategory>> listByLine(@RequestParam String productLineId) {

        return Result.ok(productDiscountCategoryService.getByProductLine(productLineId));
    }

    @ApiOperation(value = "分类管理-查询单条", notes = "分类管理-查询单条")
    @RequestMapping(value = "/queryById", method = RequestMethod.GET)
    @PreAuthorize("hasAuthority('" + AdminAuthConstants.ProductDiscountCategory.EDIT + "')")
    public Result<ProductDiscountCategory> queryById(@RequestParam String id) {

        return Result.ok(productDiscountCategoryService.getById(id));
    }

    @ApiOperation(value = "分类管理-查询优惠比例", notes = "分类管理-查询优惠比例")
    @RequestMapping(value = "/queryDiscount", method = RequestMethod.GET)
    @PreAuthorize("hasAuthority('" + AdminAuthConstants.ProductDiscountCategory.EDIT + "')")
    public Result<List<ProductDiscountCategoryDetail>> queryDiscount(@RequestParam String id) {

        return Result.ok(productDiscountCategoryLevelService.getByDiscountCategory(id));
    }

    @ApiOperation(value = "分类管理-保存或编辑", notes = "分类管理-保存或编辑")
    @RequestMapping(value = "/saveOrUpdate", method = RequestMethod.POST)
    @PreAuthorize("hasAuthority('" + AdminAuthConstants.ProductDiscountCategory.EDIT + "')")
    public Result<ProductDiscountCategory> saveOrUpdate(@RequestBody ProductDiscountCategory productDiscountCategory) {

        return Result.ok(productDiscountCategoryService.saveOrUpdateSaveObject(productDiscountCategory, true));
    }

    @ApiOperation(value = "分类管理-删除", notes = "分类管理-删除")
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    @PreAuthorize("hasAuthority('" + AdminAuthConstants.ProductDiscountCategory.EDIT + "')")
    public Result<Boolean> delete(@RequestParam String id) {

        return Result.ok(productDiscountCategoryService.checkAndRemoveById(id));
    }

    @ApiOperation(value = "根据等级查询优惠信息", notes = "根据等级查询优惠信息")
    @RequestMapping(value = "/getDiscountByLevel", method = RequestMethod.GET)
    public Result<List<ProductDiscountCategoryDetail>> getDiscountByLevel(@RequestParam String productLineLevelId) {

        return Result.ok(productDiscountCategoryService.getDiscountByLevel(productLineLevelId));
    }


}
